/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.web.api;

import com.jfinal.aop.Inject;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.JsTicket;
import com.jfinal.weixin.sdk.api.JsTicketApi;
import io.jboot.apidoc.annotation.Api;
import io.jboot.apidoc.annotation.ApiOper;
import io.jboot.apidoc.annotation.ApiResp;
import io.jboot.web.controller.annotation.PostRequest;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jpress.service.UserService;
import io.jpress.web.base.ApiControllerBase;

import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * 微信相关 API
 */
@RequestMapping("/api/wechat")
@Api("微信相关 API")
public class WechatApiController extends ApiControllerBase {

    @Inject
    private UserService userService;


    @PostRequest
    @ApiOper("获取用户分享信息")
    @ApiResp(field = "shareUrl", notes = "分享的 url 地址", mock = "https://www.***.com/xxx/xxx")
    public Ret getShareConfig(@NotEmpty String shareUrl) {
        shareUrl = shareUrl.split("#")[0];

        final ApiConfig apiConfig = ApiConfigKit.getApiConfig();

        ApiConfigKit.setThreadLocalAppId(apiConfig.getAppId());
        String _wxJsApiTicket = "";
        try {
            JsTicket jsTicket = JsTicketApi.getTicket(JsTicketApi.JsApiType.jsapi);
            _wxJsApiTicket = jsTicket.getTicket();
        } finally {
            ApiConfigKit.removeThreadLocalAppId();
        }
        Map<String, String> signatureMap = new TreeMap<>();

        //noncestr
        String nonceStr = StrKit.getRandomUUID();
        //timestamp
        String timestamp = (System.currentTimeMillis() / 1000) + "";

        //参数封装
        signatureMap.put("noncestr", nonceStr);
        signatureMap.put("timestamp", timestamp);
        signatureMap.put("jsapi_ticket", _wxJsApiTicket);
        signatureMap.put("url", shareUrl);

        // 加密获取signature
        StringBuilder signatureData = new StringBuilder();
        for (Map.Entry<String, String> param : signatureMap.entrySet()) {
            signatureData.append(param.getKey()).append("=").append(param.getValue()).append("&");
        }

        // signature
        String signature = HashKit.sha1(signatureData.substring(0, signatureData.length() - 1));

        Map<String, Object> retMap = new HashMap<>();
        retMap.put("debug", getBoolean("debug", false));
        retMap.put("appId", apiConfig.getAppId());
        retMap.put("timestamp", timestamp);
        retMap.put("nonceStr", nonceStr);
        retMap.put("signature", signature);

        //这部分，前端可以自己添加和封装
        //retMap.put("jsApiList","....")

        return Ret.ok().set(retMap);
    }

}
